/*
 *  This file is part of LifeBudget - Copyright 2010 Clifton Eaton
 *  ------------------------------------------------------------------
 *  LifeBudget is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  LifeBudget is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with LifeBudget.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.lifebudget.app.server;

import java.util.List;

import org.lifebudget.app.client.mvc.model.CategoryAmount;
import org.lifebudget.app.client.mvc.model.PaycheckCalculator;
import org.lifebudget.app.client.service.LoginService;
import org.lifebudget.app.client.service.PaycheckCalculatorService;
import org.lifebudget.app.persistence.repository.PaycheckCalculatorRepository;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * The Class PaycheckCalculatorServiceImpl implements the {@link PaycheckCalculatorService} interface
 * and provides server-side functionality for GWT RPC.
 */
public class PaycheckCalculatorServiceImpl extends RemoteServiceServlet implements PaycheckCalculatorService {
	
	/** The paycheck repository. */
	PaycheckCalculatorRepository paycheckRepo;

	/**
	 * Instantiates a new paycheck calculator service.
	 */
	public PaycheckCalculatorServiceImpl() {
		paycheckRepo = new PaycheckCalculatorRepository();
	}

	/* (non-Javadoc)
	 * @see org.lifebudget.app.client.service.PaycheckCalculatorService#getAllPaycheckCalculators()
	 */
	@Override
	public List<PaycheckCalculator> getAllPaycheckCalculators() {

		return paycheckRepo.getAllPaycheckCalculators();
	}

	/* (non-Javadoc)
	 * @see org.lifebudget.app.client.service.PaycheckCalculatorService#addPaycheckCalculator(org.lifebudget.app.client.mvc.model.PaycheckCalculator)
	 */
	@Override
	public PaycheckCalculator addPaycheckCalculator(PaycheckCalculator paycheck) {
		System.out.println("In PaycheckCalculatorServiceImpl storing " + paycheck.getName());
		return paycheckRepo.addPaycheckCalculator(paycheck);
	}

	/* (non-Javadoc)
	 * @see org.lifebudget.app.client.service.PaycheckCalculatorService#deletePaycheckCalculator(org.lifebudget.app.client.mvc.model.PaycheckCalculator)
	 */
	@Override
	public void deletePaycheckCalculator(PaycheckCalculator paycheck) {
		paycheckRepo.deletePaycheckCalculator(paycheck);
	}

	/* (non-Javadoc)
	 * @see org.lifebudget.app.client.service.PaycheckCalculatorService#removeCategoryAmount(org.lifebudget.app.client.mvc.model.CategoryAmount)
	 */
	@Override
	public void removeCategoryAmount(CategoryAmount categoryAmount) {
		paycheckRepo.deleteCategoryAmount(categoryAmount);

	}

	/* (non-Javadoc)
	 * @see org.lifebudget.app.client.service.PaycheckCalculatorService#updateCategoryAmounts(java.lang.Long, java.util.List)
	 */
	@Override
	public List<CategoryAmount> updateCategoryAmounts(Long payCalcId, List<CategoryAmount> categoryAmounts) {
		if (categoryAmounts != null && !categoryAmounts.isEmpty()) {
			paycheckRepo.updateCategoryAmounts(categoryAmounts);

		}
		return paycheckRepo.getAllRelatedCategoryAmounts(payCalcId);
	}

	/* (non-Javadoc)
	 * @see org.lifebudget.app.client.service.PaycheckCalculatorService#updatePaycheckCalculator(org.lifebudget.app.client.mvc.model.PaycheckCalculator)
	 */
	@Override
	public void updatePaycheckCalculator(PaycheckCalculator paycheck) {
		paycheckRepo.updatePaycheckCalculator(paycheck);

	}

}
